/*
 * BASocketClientEx.java
 * classes : com.qim.basdk.network.BASocketClientEx
 * @author xuwei
 * V 1.0.0
 * Created at 2015年9月6日 上午8:59:13
 */
package com.langsheng.lsintell.network;

import com.langsheng.lsintell.cmd.data.LSDataBuffer;
import com.langsheng.lsintell.cmd.request.LSRequest;
import com.langsheng.lsintell.cmd.response.LSResponse;
import com.langsheng.lsintell.data.LSServerInfo;
import com.langsheng.lsintell.interfaces.LSResponseListener;
import com.langsheng.lsintell.utils.LSLog;

/**
 * com.qim.basdk.network.LSSocketClientEx
 * <p>
 * socket短连接,维护
 *
 * @author xuwei
 *         <p>
 *         created at 2015年9月6日 上午8:59:13
 */
public class LSSocketClientEx {
    public final static String TAG = "LSSocketClientEx";

    private LSSocketWrapper socket;

    /**
     * 创建一个socket
     */
    public LSSocketClientEx() {
        socket = new LSSocketWrapper();
    }

    /**
     * 获得socket连接状态
     *
     * @return
     */
    public boolean isConnected() {
        return socket.isConnected();
    }

    /**
     * 连接服务器,超时3s
     *
     * @param server
     * @return
     */
    public synchronized boolean connect(LSServerInfo server) {
        socket.connect(server.getIp(), server.getPort(), 3000);
        return socket.isConnected();
    }

    /**
     * 仅向服务器发送数据
     *
     * @param request
     * @return
     * @throws Exception
     */
    public int sendRequest(LSRequest request) throws Exception {
        if (!socket.isConnected()) {
            return 0;
        }
        LSLog.e(TAG, "" + request.toString());
        return socket.write(request.toBytes());
    }

    /**
     * 仅接收服务器发回来的单个数据包
     *
     * @return
     * @throws Exception
     */
    public LSResponse getResponse() throws Exception {
        if (!socket.isConnected()) {
            return null;
        }
        byte[] tmp = new byte[8 * 1024];
        LSDataBuffer buffer = new LSDataBuffer();
        while (true) {
            int len = socket.read(tmp);
            buffer.writeByteArray(tmp, 0, len);
            if (buffer.checkCommand()) {
                break;
            }
        }
        LSResponse res = new LSResponse(buffer.readFirstCmd());
        LSLog.e(TAG, "" + res.toString());
        return res;
    }

    /**
     * 向服务器发送请求,读取服务器响应后返回
     *
     * @param request
     * @return
     * @throws Exception
     */
    public LSResponse getSingleResponse(LSRequest request) throws Exception {
        int length = sendRequest(request);
        if (length > 0) {
            return getResponse();
        }
        return null;
    }

    /**
     * 获取服务器发送的连续的数据包
     *
     * @param request
     * @param listener
     * @throws Exception
     */
    public void getResponse(LSRequest request, LSResponseListener listener) throws Exception {
        if (!socket.isConnected()) {
            return;
        }
        byte[] tmp = new byte[8 * 1024];
        LSDataBuffer buffer = new LSDataBuffer();

        sendRequest(request);
        while (true) {
            int len = socket.read(tmp);
            buffer.writeByteArray(tmp, 0, len);
            while (buffer.checkCommand()) {
                LSResponse res = new LSResponse(buffer.readFirstCmd());
                LSLog.e(TAG, "" + res.toString());
                if (listener.onResponse(res)) {
                    return;
                }
            }
        }
    }

    /**
     * 关闭socket连接
     */
    public void close() {
        socket.close();
    }

}
